package cn.sunline.cas.config;

import cn.sunline.cas.authentication.MyAuthenticationHandler;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlan;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlanConfigurer;
import org.apereo.cas.authentication.AuthenticationHandler;
import org.apereo.cas.authentication.principal.DefaultPrincipalFactory;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.model.support.jdbc.QueryEncodeJdbcAuthenticationProperties;
import org.apereo.cas.configuration.model.support.jdbc.QueryJdbcAuthenticationProperties;
import org.apereo.cas.services.ServicesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableConfigurationProperties({CasConfigurationProperties.class})
public class MyAuthenticationHandlerPlanConfigurer implements AuthenticationEventExecutionPlanConfigurer {

    @Resource
    private CasConfigurationProperties casProperties;

    @Resource
    private ServicesManager servicesManager;


    @Bean
    public DataSource queryDataSource() {
        QueryJdbcAuthenticationProperties query = casProperties.getAuthn().getJdbc().getQuery().get(0);
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(query.getDriverClass());
        dataSource.setUrl(query.getUrl());
        dataSource.setUsername(query.getUser());
        dataSource.setPassword(query.getPassword());
        return dataSource;
    }

    /**
     * 创建自定义的AuthenticationHandler对象
     * 第一个参数 是对象名 myAuthenticationHandler
     * 第二个参数是cas自己创建的对象servicesManager
     */
    @Bean
    public AuthenticationHandler myAuthenticationHandler(){
        QueryJdbcAuthenticationProperties query = casProperties.getAuthn().getJdbc().getQuery().get(0);
        // 注册
        Map<String, String> properties = casProperties.getCustom().getProperties();
        String aesKey = properties.get("aesKey");
        String aesVi = properties.get("aesVi");
        // 获取自定义结果
        return new MyAuthenticationHandler(MyAuthenticationHandler.class.getSimpleName(),
                servicesManager, jdbcPrincipalFactory(),1, queryDataSource(), query.getPasswordEncoder().getEncodingAlgorithm(),
                query.getSql(), query.getFieldPassword(), query.getFieldExpired(), query.getFieldDisabled(), aesKey, aesVi);
    }

    @ConditionalOnMissingBean(name = "jdbcPrincipalFactory")
    @Bean
    @RefreshScope
    public PrincipalFactory jdbcPrincipalFactory() {
        return new DefaultPrincipalFactory();
    }

    @Override
    public void configureAuthenticationExecutionPlan(AuthenticationEventExecutionPlan plan) {
        plan.registerAuthenticationHandler(myAuthenticationHandler());
    }
}
